package org.saratogaschools.edd.firefighter.client.view;

import org.saratogaschools.edd.firefighter.shared.model.Department;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.SingleSelectionModel;

public class SelectDepartmentView extends Composite {

	public interface Presenter {

		void addDepartment();

		void selectDepartment(Department dpt);
		
	}

	private static SelectDepartmentViewUiBinder uiBinder = GWT
			.create(SelectDepartmentViewUiBinder.class);

	interface SelectDepartmentViewUiBinder extends
			UiBinder<Widget, SelectDepartmentView> {
	}

	private Presenter presenter;
	
	@UiField
	SpanElement nameSpan;
	
	@UiField(provided=true)
	CellList<Department> cellList;
	
	SingleSelectionModel<Department> selectionModel;
	
	@UiField
	Button addDepartmentButton;
	
	@UiField
	Button nextButton;

	private boolean addEnabled;
	
	public SelectDepartmentView() {
		super();
		this.cellList = new CellList<Department>(new DepartmentCell());
		cellList.setEmptyListWidget(new Label("No departments created yet."));
		this.selectionModel = new SingleSelectionModel<Department>();
		cellList.setSelectionModel(selectionModel);
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	public void setName(String name) {
		nameSpan.setInnerText(name);
	}

	public HasData<Department> getDepartmentDisplay() {
		return cellList;
	}
	
	@UiHandler("addDepartmentButton")
	void onAddDepartmentButtonClicked(ClickEvent evt) {
		addDepartmentButton.setEnabled(false);
		presenter.addDepartment();
	}
	
	@UiHandler("nextButton")
	void onNextButtonClicked(ClickEvent evt) {
		nextButton.setEnabled(false);
		addDepartmentButton.setEnabled(false);
		Department dpt = selectionModel.getSelectedObject();
		if (dpt != null) {
			presenter.selectDepartment(dpt);
		}
		else {
			nextButton.setEnabled(true);
			addDepartmentButton.setEnabled(addEnabled);
		}
	}
	
	private class DepartmentCell extends AbstractCell<Department> {

		@Override
		public void render(com.google.gwt.cell.client.Cell.Context context,
				Department value, SafeHtmlBuilder sb) {
			if (value == null) {
				return;
			}
			sb.appendEscaped(value.getName());
		}
		
	}

	public void setNewDepartmentButtonEnabled(boolean addEnabled) {
		this.addEnabled = addEnabled;
		this.addDepartmentButton.setEnabled(addEnabled);
	}
}
